<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 消息提示 -->
       <el-alert
        title="编辑商品信息"
        type="info"
        show-icon
        center>
      </el-alert>
      <!-- 表单 -->
      <el-form :model="editForm"
               :rules="editFormRules"
               ref="editFormRef" label-width="100px"
               label-position="top">
        <!-- 标签页 -->
        <el-tabs v-model="activeIndex"
                 tab-position="left"
                 @tab-click="tabClick">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="活动名称" prop="goods_name">
              <el-input v-model="editForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="editForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="editForm.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="editForm.goods_number"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="1">
            <el-upload
              action="http://127.0.0.1:8888/api/private/v1/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              list-type="picture"
              :headers="headerObj"
              :on-success="handleSuccess">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="2">
            <!-- 富文本编辑器 -->
            <quill-editor v-model="editForm.goods_introduce"></quill-editor>
            <!-- 编辑商品按钮 -->
            <el-button type="primary" @click="edit">修改商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <!-- 预览图片区域 -->
    <el-dialog
      title="图片预览"
      :visible.sync="previewDialog"
      width="50%"
      class="picss">
      <img :src="previewPicture" alt="">
    </el-dialog>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  data() {
    return {
      //商品ID
      goods_id: '',
      //tab页签当前选中的index
      activeIndex: '0',
      //商品列表
      editForm: {},
      //商品表单验证规则
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ]
      },
      //图片信息
      fileList: [],
      //配置token
      headerObj: {
        Authorization: window.sessionStorage.getItem('token')
      },
      //图片地址
      previewPicture: '',
      //控制图片预览的显示与隐藏
      previewDialog: false
    }
  },
  created() {
    this.goods_id = this.$route.query.id
    console.log(this.goods_id);
    if(!this.goods_id) {
      return this.$message.error('获取商品id失败')
    }
    this.getGoods()
  },
  methods: {
    //根据id获取对应商品信息
    async getGoods() {
      const {data: res} = await this.$http.get('goods/' + this.goods_id)
      if(res.meta.status !== 200) {
        return this.$message.error('获取商品信息失败')
      }
      this.editForm = res.data
      console.log(this.editForm);
      //遍历pics拿到图片信息
      this.editForm.pics.forEach(item => {
        const newInfo = {
          name: item.pics_id,
          url: item.pics_big_url
        }
        this.fileList.push(newInfo)
      })
    },
    //标签页点击事件
    tabClick() {},
    //点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      if(file.status === "success") {
        this.previewPicture = file.url
        this.previewDialog = true
      }
    },
    //文件列表移除文件时的钩子
    handleRemove(file) {
      console.log(file);
    },
    //文件上传成功时的钩子
    handleSuccess(response) {
      console.log(response);
    },
    //点击按钮修改该商品信息
    edit() {}
  }
}
</script>

<style scoped>
.el-tabs {
  margin-top: 15px;
}
.picss img{
  width: 100%;
}
</style>
